import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Axios from 'axios'
import 'element-ui/lib/theme-chalk/index.css'
import ElementUI from 'element-ui'
import utils from './assets/js/utils'
import './assets/css/common.css'

Vue.config.productionTip = false

let instance = Axios.create({
  baseURL: "/api",
  timeout: 10000
})

instance.interceptors.request.use(config => {
  config.headers.token = utils.getToken()
  return config
})

instance.interceptors.response.use(res => {
  if (res.data && !res.data.success) {
    return Promise.reject(res.data)
  }
  return res
}, err => {
  if (err.response && (err.response.status === 401) || err.response.status == 500) {
    // 定向到登录页面
    vm.$router.replace('/login')
  }
  return Promise.reject(err)
})

Vue.use(ElementUI)
Vue.prototype.$axios = instance
Vue.prototype.$utils = utils

let vm = new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
